package com.c.ctgproject.ui.production.pre

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.utils.*
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.common.Constant
import kotlinx.android.synthetic.main.activity_pre_production.*
import org.json.JSONObject

/**
 *作者:LiBW
 *创建日期: 2020/9/4
 *描述:生产前 扫描结果页面
 */
class PreProductionActivity : BaseActivity() {
    //二维码回调
    private val requestCodeScan = 10001
    private val mViewModel: PreProductionViewModel by lazy {
        ViewModelProvider(this)[PreProductionViewModel::class.java]
    }
    private var carCode = ""
    private var weightBeforeDis = ""

    override fun getLayout() = R.layout.activity_pre_production

    override fun event() {
        changeTransparentStatusBar()
        val data = intent.getStringExtra("Message")
        data?.let {
            MyLog.d(it)
            val entity = JSONObject(it)
            mBianHao.text = entity.getString("carCode").handlerNull()
            mOwnerPlate.text = entity.getString("ownerPlate").handlerNull()
            carCode = entity.getString("carCode").handlerNull()
            mWeightTV.text = entity.getString("weightBeforeDis").handlerNull()
        }
        //扫码回调
        mViewModel.scanQR.observe(this, Observer {
            loading.dismiss()
            MyLog.d("扫码回调:$it")
            val entity = initData(it)
            if (entity.status == 0) {
                showToast("操作成功")
                finish()
            } else {
                showToast(entity.msg)
            }
        })
        mViewModel.error.observe(this, Observer {
            MyLog.d("错误:${it}")
            loading.dismiss()
            CustomToast.showToast(this, it)
        })
    }

    override fun click() {
        mScanQrBTN.setOnClickListener {
            startActivityForResult(Intent(this, CaptureActivity::class.java), requestCodeScan)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            //二维码回调
            if (requestCode == requestCodeScan && data != null) {
                val content: String? = data.getStringExtra(Constant.CODED_CONTENT)
                MyLog.d("二维码扫描结果:$content")
                if (content != null) {
                    val messages: List<String> = content.split(":")
                    if (messages.size <= 1) {
                        CustomToast.showToast(this, "二维码不包含库位信息")
                    }
                    if (messages[0] == "warehouseId") {
                        loading.show()
                        mViewModel.scanQR(carCode, messages[1], mWeightTV.text.handlerNull())
                    } else {
                        CustomToast.showToast(this, "请扫车间二维码")
                    }
                }
            }
        }
    }
}